package com.zm.core.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import javax.servlet.ServletContext;

import com.jfinal.core.JFinal;

public class ZmUtil {
	/**
	 * 获取图片的路径
	 * @param url
	 * @return
	 */
	public static String getFilePath(String url){
		ServletContext servletContext = JFinal.me().getServletContext();
		String relurl=servletContext.getRealPath(url); 
		return relurl;
	}

	/**
	 * 利用字节流读文件
	 * 
	 * @param file
	 *            源文件
	 * @return 源文件的字符串
	 */
	public static byte[] readerFile(File file) {
		if (file.isFile()) {
			byte[] b=null;
			FileInputStream fileInputStream = null;
			try {
				fileInputStream = new FileInputStream(file);
				// fileInputStream.available()获取文件的字节数
				b = new byte[fileInputStream.available()];
				int read = fileInputStream.read(b);
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				try {
					fileInputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			return b;
		}
		return null;
	}
}
